package com.example.controller;

import com.example.entity.User;
import com.example.mapper.UsersMapper;
import com.example.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;

// 添加事务注解
import org.springframework.transaction.annotation.Transactional;

@RestController
@RequestMapping("/api/users")
public class UserController {
    @Autowired
    private UsersMapper usersMapper;

    @PutMapping("/update")
    @Transactional // 事务管理确保所有更新同时成功或失败
    public Result updateUserInfo(@RequestBody User user) {
        try {
            // 验证必要参数
            if (user == null) {
                return Result.error("用户信息不能为空");
            }
            if (user.getPhone() == null || user.getPhone().trim().isEmpty()) {
                return Result.error("手机号不能为空");
            }
            if (user.getUsername() == null || user.getUsername().trim().isEmpty()) {
                return Result.error("用户名不能为空");
            }
            
            // 获取旧用户名
            User oldUser = usersMapper.findByPhone(user.getPhone());
            if (oldUser == null) {
                return Result.error("用户不存在");
            }
            
            String oldUsername = oldUser.getUsername();
            String newUsername = user.getUsername();
            
            // 执行三个表的更新操作
            int userResult = usersMapper.updateUsernameInUsers(user.getPhone(), newUsername);
            int groupResult = usersMapper.updateUsernameInGroups(oldUsername, newUsername);
            int projectResult = usersMapper.updateUsernameInProjects(oldUsername, newUsername);
            
            if(userResult > 0 && groupResult >= 0 && projectResult >= 0) {
                User updatedUser = usersMapper.findByPhone(user.getPhone());
                return Result.success(updatedUser);
            }
            return Result.error("更新失败");
        } catch (Exception e) {
            return Result.error("服务器错误: " + e.getMessage());
        }
    }
    
    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestBody User user) {
        try {
            // 验证必要字段
            if (user == null) {
                return Result.error("用户信息不能为空");
            }
            if (user.getPhone() == null || user.getPhone().trim().isEmpty()) {
                return Result.error("手机号不能为空");
            }
            if (user.getPassword() == null || user.getPassword().trim().isEmpty()) {
                return Result.error("原密码不能为空");
            }
            if (user.getNewPassword() == null || user.getNewPassword().trim().isEmpty()) {
                return Result.error("新密码不能为空");
            }
            
            // 更新密码
            int result = usersMapper.updatePasswordByPhone(user);
            if(result > 0) {
                return Result.success("密码修改成功");
            } else {
                return Result.error("原密码错误或用户不存在");
            }
        } catch (Exception e) {
            return Result.error("服务器错误: " + e.getMessage());
        }
    }
    
    // 添加用户搜索接口
    @GetMapping("/search")
    public Result searchUsers(
            @RequestParam(required = false) String username,
            @RequestParam(required = false) String phone) {
        try {
            List<User> users = usersMapper.searchUsers(username, phone);
            return Result.success(users);
        } catch (Exception e) {
            return Result.error("搜索用户失败: " + e.getMessage());
        }
    }
}